Conditions | 2 |
Paths | 2 |
Total Lines | 67 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
41 | export function formToSky(userOptions = {}) { |
||
42 | |||
43 | |||
44 | var options = { |
||
45 | selector: '.ajax-form', // selector for ajax form |
||
46 | }; |
||
47 | for (var attrname in userOptions) { |
||
48 | options[attrname] = userOptions[attrname]; |
||
49 | } |
||
50 | |||
51 | document.querySelectorAll(options.selector).forEach(item => { |
||
52 | if (item.querySelector('form') !== null) { |
||
53 | item.querySelector('form').addEventListener("submit", e => { |
||
54 | e.preventDefault(); |
||
55 | sendFormToSky(e); |
||
56 | }); |
||
57 | } |
||
58 | }); |
||
59 | |||
60 | var sendFormToSky = function(form) { |
||
61 | var $submitButton = getSubmitButton(form); |
||
62 | if ($submitButton !== null) { |
||
63 | var initialButton = getSubmitButton(form).outerHTML; |
||
64 | $submitButton.outerHTML = '<i class="fa fa-spinner fa-spin"></i>'; |
||
65 | } |
||
66 | |||
67 | //var formData = new FormData(); |
||
68 | var toSend = ''; |
||
69 | for (var i = 0; i < form.srcElement.length; ++i) { |
||
70 | toSend += encodeURI(form.srcElement[i].name)+"="+encodeURI(form.srcElement[i].value)+'&'; |
||
71 | } |
||
72 | |||
73 | var xmlhttp = new XMLHttpRequest(); |
||
74 | xmlhttp.addEventListener("load", function() { |
||
75 | form.srcElement.outerHTML = xmlhttp.responseText; |
||
76 | formToSky(); |
||
77 | }, false); |
||
78 | xmlhttp.open("POST",form.srcElement.action,false); |
||
79 | xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
||
80 | xmlhttp.send(toSend); |
||
81 | } |
||
82 | |||
83 | var renderError = function(error) { |
||
84 | var msg = ''; |
||
85 | for (var key in error) { |
||
86 | if (error.hasOwnProperty(key)) { |
||
87 | var obj = error[key]; |
||
88 | for (var prop in obj) { |
||
89 | if (obj.hasOwnProperty(prop)) { |
||
90 | msg += key + " : " + obj[prop] + '<br>'; |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | return msg; |
||
96 | } |
||
97 | |||
98 | var getSubmitButton = function(form) { |
||
99 | if (form.srcElement.querySelector('[type=submit]') !== null) { |
||
100 | return form.srcElement.querySelector('[type=submit]'); |
||
101 | } |
||
102 | if (form.srcElement.getElementsByTagName('button') !== null) { |
||
103 | return form.srcElement.getElementsByTagName('button'); |
||
104 | } |
||
105 | return null; |
||
106 | } |
||
107 | } |
||
108 | |||
155 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.